package sis.studentinfo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

public class Student implements Serializable{

	public enum Grade {
		A(4), B(3), C(2), D(1), F(0);
		
		private int points;
		Grade(int points) {
			this.points = points;
		}
		
		public int getPoints() {
			return this.points;
		}
	}
	
	
	private String name;
	private int credits;
	private String state = "";
	private List<Grade> grades;
	private GradingStrategy gradingStrategy;
	private String firstName;
	private String middleName = "";
	private String lastName = "";
	
	public Student() {}
	public Student(String name) {
		this.name = name;
		this.grades = new ArrayList<Grade>();
		gradingStrategy = new BasicGradingStrategy();
		List<String> nameParts = split(name);
		if(nameParts.size() > MAX_NAME_PARTS) {
			String message = String.format(TOO_MANY_NAME_PARTS_MSG, name, MAX_NAME_PARTS);
			Student.logger.info(message);
			throw new StudentNameFormatException(message);
		}
		this.setName(nameParts);
	}

	private void setName(List<String> nameParts) {
		if(nameParts.size() == 1) {
			this.lastName = nameParts.get(0);
		} else if(nameParts.size() == 2) {
			this.firstName = nameParts.get(0);
			this.lastName = nameParts.get(1);
		} else if(nameParts.size() == 3) {
			this.firstName = nameParts.get(0);
			this.middleName = nameParts.get(1);
			this.lastName = nameParts.get(2);
		}
	}
	
	private List<String> split(String string) {
		List<String> results = new ArrayList<String>();
		for(String s : string.split(" ")) {
			results.add(s);
		}
		return results;
	}

	public static final Logger logger = Logger.getLogger(Student.class.getName());
	public static final int CREDITS_REQUIRED_FOR_FULL_TIME = 12;
	private static final long serialVersionUID = 1L;
	public static final String IN_STATE = "CO";
	public static final int MAX_NAME_PARTS = 3;
	public static final String TOO_MANY_NAME_PARTS_MSG = "Student name '%s' contains more than %d parts";
	
	public double getGpa() {
		if(grades.isEmpty()) {
			return 0;
		}
		double total = 0.0;
		for(Grade grade : grades) {
			total += this.gradingStrategy.getGradePonitsFor(grade);
		}
		return total / this.grades.size();
	}
	
	public void addGrade(Grade a) {
		this.grades.add(a);
	}

	public String getName() {
		return this.name;
	}
	
	public boolean isFullTime() {
		return this.credits >= CREDITS_REQUIRED_FOR_FULL_TIME;
	}
	
	public int getCredits() {
		return this.credits;
	}
	
	public void addCreadits(int i) {
		this.credits += i;
	}
	public boolean inInState() {
		return this.state.equals(Student.IN_STATE);
	}
	public void setState(String state) {
		this.state = state;
	}
	public void setGradingStrategy(GradingStrategy gradingStrategy) {
		this.gradingStrategy = gradingStrategy;
	}
	
	public String getFirstName() {
		return this.firstName;
	}
	
	public String getLastName() {
		return this.lastName;
	}
	
	public String getMiddleName() {
		return this.middleName;
	}
	

}
